using System;
using System.Data;
using System.Text;
using BrightFrame;
using AIM.Model;
namespace AIM.DAL
{
	public class RoleDAL
	{
        private DbProvider dbProvider = new DbProvider();

        public Role GetModel(int roleID)
        {
            Role role = null;
            using (IDataReader dr = dbProvider.ExecuteDataReader("SELECT RoleID,RoleName,Description FROM role WHERE RoleID=?RoleID",
                dbProvider.CreateParameter("?RoleID", DbType.Int32, roleID)))
            {
                if (dr.Read())
                {
                    role = new Role
                    {
                        RoleID = Convert.ToInt32(dr["RoleID"]),
                        RoleName = dr["RoleName"].ToString(),
                        Description = dr["Description"].ToString()
                    };
                }
            }
            return role;
        }

        public void Add(Role role)
        {
            dbProvider.ExecuteNonQuery("INSERT INTO role (RoleName,Description) VALUES (?RoleName,?Description)",
                dbProvider.CreateParameter("?RoleName", DbType.AnsiString, role.RoleName),
                dbProvider.CreateParameter("?Description", DbType.AnsiString, role.Description));
        }

        public void Update(Role role)
        {
            dbProvider.ExecuteNonQuery("UPDATE role SET RoleName=?RoleName, Description=?Description WHERE RoleID=?RoleID",
                dbProvider.CreateParameter("?RoleID", DbType.Int32, role.RoleID),
                dbProvider.CreateParameter("?RoleName", DbType.AnsiString, role.RoleName),
                dbProvider.CreateParameter("?Description", DbType.AnsiString, role.Description));
        }

        public void Delete(int roleID)
        {
            dbProvider.ExecuteNonQuery("DELETE FROM role WHERE RoleID=?RoleID",
                dbProvider.CreateParameter("?RoleID", DbType.Int32, roleID));
        }

        public DataTable QueryAll()
        {
            return dbProvider.ExecuteDataTable("SELECT RoleID,RoleName FROM role");
        }
	}
}

